package com.qipay.mapper;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SpChannelRuleSqlProvider extends com.qipay.baas.mapper.SpChannelRuleSqlProvider {

    public String countByCondition(Map<String, Object> params) {
        if (params != null && params.size() > 0) {
            List<String> lst = buildAgentIdSql(params);
            String sql = " select count(0) from `sp_channel_rule` where 1=1 and  " + StringUtils.join(lst.toArray(), " and ");
            return sql ;

        }
        return "SELECT count(0) FROM `sp_channel_rule` LIMIT 1";
    }

    public String selectByCondition(Map<String, Object> params) {
        if (params != null && params.size() > 2) {
            List<String> lst = buildAgentIdSql(params);
            String sql = " select * from `sp_channel_rule`  where  1=1 and   " + StringUtils.join(lst.toArray(), " and ");
            sql += " ORDER BY `sp_id` desc LIMIT #{offset},#{size}";
            return sql;
        }
        return "SELECT * FROM `sp_channel_rule`  ORDER BY `sp_id` desc LIMIT #{offset},#{size}";

    }

    public String getBySpId(Map<String, Object> params) {
        List<String> lst = buildAgentIdSql(params);
        String sql = " select * from `sp_channel_rule`  where  1=1 and   " + StringUtils.join(lst.toArray(), " and ");
        return sql;
    }

    List<String> buildAgentIdSql(Map<String, Object> params) {
        List<String> lst = new ArrayList<>(4);
        if (params.getOrDefault("spId", null) != null) {
            lst.add("`sp_id` =#{spId}");
        }

        return lst;
    }

}